<template>
  <!-- 头部面包屑 -->
  <div class="header">
    <el-breadcrumb :separator-icon="ArrowRight">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>{{ menuName }}</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <!-- 搜索框 -->
  <div class="search">
    <el-form :inline="true" v-bind:model="formInline" class="demo-form-inline">
      <el-form-item label="门店名称">
        <el-input v-model="formInline.keyword" placeholder="请输入门店名称" clearable />
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="formInline.phone" placeholder="请输入手机号" clearable />
      </el-form-item>
      <el-form-item label="时间">
        <el-date-picker
          v-model="formInline.time"
          type="daterange"
          start-placeholder="Start date"
          end-placeholder="End date"
          :default-time="defaultTime"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="toAdd">新增</el-button>
  </div>
  <!-- 表格 -->
  <div class="dataList">
    <el-table :data="tableData" style="width: 100%">
      <el-table-column fixed prop="shopId" label="门店id" width="150" />
      <el-table-column prop="shopName" label="门店名称" width="120" />
      <el-table-column prop="shopPhone" label="门店手机号" width="120" />
      <el-table-column prop="shopCity" label="所在城市" width="120" />

      <el-table-column fixed="right" label="操作" width="120">
        <template v-slot="scope">
          <el-button link type="primary" size="small" @click="handleClick(scope.row)"
            >修改</el-button
          >
          <el-button link type="primary" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="currentPage1"
      @current-change="handleCurrentChange"
      :background="backgroundFlag"
      layout="prev, pager, next"
      :total="total"
    />
  </div>

  <!-- 新增，修改dialog 弹出框 -->
  <el-dialog v-model="dialogFormVisible" title="新增/修改">
    <el-form :model="form">
      <el-form-item label="店铺名称" :label-width="formLabelWidth">
        <el-input v-model="form.shopName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="手机号" :label-width="formLabelWidth">
        <el-input v-model="form.shopPhone" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="addOrModify"> 确认 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { ArrowRight } from '@element-plus/icons-vue'
import { ref, reactive, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import axios from 'axios'
//获取路由参数值，在header中渲染
var menuName = ref('')
var route = useRoute()
var total = ref(0)
var currentPage1 = ref(1)
var backgroundFlag = ref(true)
menuName.value = route.params.name
const dialogFormVisible = ref(false)
const formInline = reactive({
  pageNum: 1,
  pageSize: 10,
  phone: '',
  keyword: '',
})
const form = ref({})
const value = ref('')
const tableData = ref([])
const defaultTime =
  ref < [Date, Date] > [new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)]
//分页
function handleCurrentChange(page) {
  console.log(page)
  //发起网络请求，查询相关数据
  formInline.pageNum = page
  search()
}
const search = async () => {
  //接收数据
  console.log(formInline)
  //发起网络请求
  let url = 'http://43.143.190.87:8091/panghudaojiaBackend/shop/querySearchShopList'
  const response = await axios.get(url, { params: formInline })
  //解析数据
  console.log(response.data.data)
  //展示数据
  tableData.value = response.data.data.list
  //设置总共数据条数
  let t = response.data.data.total
  total.value = t
  currentPage1.value = response.data.data.pageNum
}
//页面加载完毕，调用search查询
onMounted(() => {
  search()
})
//点击form搜索执行查询
const onSubmit = async () => {
  formInline.pageNum = 1
  search()
}
//编辑
function handleClick(row) {
  console.log(row)
  //打开弹出框
  dialogFormVisible.value = true
  //添加默认值
  form.value = row
}
//新增
function toAdd() {
  //打开弹出框
  dialogFormVisible.value = true
  //添加默认值
  form.value = {}
}
//保存事件 同步到数据库表
const addOrModify = async () => {
  console.log(form.value) //true
  let url = ''
  if (form.value.shopId == null) {
    //新增
    url = 'http://43.143.190.87:8091/panghudaojiaBackend/shop/addShop'
  } else {
    //修改
    url = 'http://43.143.190.87:8091/panghudaojiaBackend/shop/modifyShop'
  }
  //发起网络请求
  const response = await axios.post(url, form.value)
  //打印响应结果
  if (response.status == 200) {
    alert('操作成功！')
    dialogFormVisible.value = false
    // search();
  }
}
</script>
<style scoped>
.header {
  height: 60px;
  background-color: aliceblue;
  box-sizing: border-box;
  padding-top: 20px;
}
.search {
  height: 80px;
}
.dataList {
  height: 800px;
  background-color: gainsboro;
}
</style>
